package lvbu.wang.lvbuoperator.utils;

import android.os.Environment;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * Created by DaiQing.Zha on 2016/11/5 0005.
 */
public class FileUtil {

    /**
     * 将文件转化为二进制数组
     * @param sdCardDir
     * @param fileName
     * @return
     */
    public static byte[] getFileToByte(File sdCardDir, String fileName) {

        File sdFile = new File(sdCardDir, fileName);
        byte[] by = new byte[(int) sdFile.length()];
        /***********************测试*********************/
//        File sdCardDir_ = Environment.getExternalStorageDirectory();//获取SDCard目录
//        File sdFile = new File(sdCardDir_, "MINI_BIKE_OS.bin");
//        byte[] by = new byte[(int) sdFile.length()];
        /***********************测试*********************/
        try {
            InputStream is = new FileInputStream(sdFile);
            ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
            byte[] bb = new byte[2048];
            int ch;
            ch = is.read(bb);
            while (ch != -1) {
                bytestream.write(bb, 0, ch);
                ch = is.read(bb);
            }
            by = bytestream.toByteArray();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return by;
    }

    /**
     * 将文件转化为二进制数组
     * @param fileName
     * @return
     */
    public static byte[] getFileToByte(String fileName) {

        File sdCardDir = Environment.getExternalStorageDirectory();//获取SDCard目录
        File sdFile = new File(sdCardDir, fileName);
        byte[] by = new byte[(int) sdFile.length()];
        try {
            InputStream is = new FileInputStream(sdFile);
            ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
            byte[] bb = new byte[2048];
            int ch;
            ch = is.read(bb);
            while (ch != -1) {
                bytestream.write(bb, 0, ch);
                ch = is.read(bb);
            }
            by = bytestream.toByteArray();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return by;
    }


    /**
     * 将文件转化为二进制数组
     * @param fileName
     * @return
     */
    public static byte[] getFileToByteByFileName( String fileName) {

        File sdFile = new File(fileName);
        byte[] by = new byte[(int) sdFile.length()];
        /***********************测试*********************/
//        File sdCardDir_ = Environment.getExternalStorageDirectory();//获取SDCard目录
//        File sdFile = new File(sdCardDir_, "MINI_BIKE_OS.bin");
//        byte[] by = new byte[(int) sdFile.length()];
        /***********************测试*********************/
        try {
            InputStream is = new FileInputStream(sdFile);
            ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
            byte[] bb = new byte[2048];
            int ch;
            ch = is.read(bb);
            while (ch != -1) {
                bytestream.write(bb, 0, ch);
                ch = is.read(bb);
            }
            by = bytestream.toByteArray();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return by;
    }
}
